package com.andaily.domain.account.actions;

import com.andaily.domain.account.AccountRepository;
import com.andaily.domain.dto.account.AccountMonthStatisticsDTO;
import com.andaily.domain.shared.paginated.PaginatedLoader;
import com.andaily.infrastructure.mybatis.data.AccountStatisticsData;
import com.andaily.web.context.BeanProvider;

import java.util.List;
import java.util.Map;

/**
 * Date: 13-7-30
 *
 * @author Shengzhao Li
 */
public class AccountMonthStatisticsDTOLoader {

    private transient AccountRepository accountRepository = BeanProvider.getBean(AccountRepository.class);
    private AccountMonthStatisticsDTO monthStatisticsDTO;

    public AccountMonthStatisticsDTOLoader(AccountMonthStatisticsDTO monthStatisticsDTO) {
        this.monthStatisticsDTO = monthStatisticsDTO;
    }

    public AccountMonthStatisticsDTO load() {
//        updateStartMonth();
        final Map<String, Object> params = monthStatisticsDTO.queryParams();
        return monthStatisticsDTO.load(new PaginatedLoader<AccountStatisticsData>() {
            @Override
            public List<AccountStatisticsData> loadList() {
                return accountRepository.findMonthStatisticsData(params);
            }

            @Override
            public int loadTotalSize() {
                return accountRepository.totalMonthStatisticsData(params);
            }
        });
    }

//    private void updateStartMonth() {
//        String monthFormat = DateUtils.MONTH_FORMAT;
//        String startMonth = monthStatisticsDTO.getStartMonth();
//        if (StringUtils.isEmpty(startMonth)) {
//            startMonth = DateUtils.toDateText(DateUtils.now(), monthFormat);
//        } else {
//            Date month = DateUtils.getDate(startMonth, monthFormat);
//            Calendar calendar = Calendar.getInstance();
//            calendar.setTime(month);
//            calendar.add(Calendar.MONTH, -monthStatisticsDTO.getObjectsPerPage());
//            startMonth = DateUtils.toDateText(calendar.getTime(), monthFormat);
//        }
//        monthStatisticsDTO.setStartMonth(startMonth);
//    }

}
